/****************************** Module Header ******************************\
 * Module Name:  MailMessageSender.cs
 * Module Version:  1.0.0 (2011-9-28)
 * Copyright (c) AnyEB.Com (14507247@qq.com)
 * 
 * This class is used to send email by Net MailMessage dll. It supplies 
 * useful method to send a email by SMTP server.
 * 
 * This source is subject to the GPL License.
 * See http://www.gnu.org/licenses/gpl.html.
 * All other rights reserved.
 * 
 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
\***************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Mail;

namespace UCF.Core
{
    /// <summary>
    /// 邮件发送类
    /// <para>提供利用.Net MailMessage对象发送邮件的方法</para>
    /// </summary>
    public class MailMessageSender : IMail
    {
        #region IMail 成员

        /// <summary>
        /// 登录名，也就是发送邮件的邮箱
        /// </summary>
        public string From { get; set; }

        /// <summary>
        /// 接收方邮件地址
        /// </summary>
        public string To { get; set; }

        /// <summary>
        /// 邮件标题
        /// </summary>
        public string Subject { get; set; }

        /// <summary>
        /// 邮件内容
        /// </summary>
        public string Body { get; set; }

        /// <summary>
        /// 附件文件
        /// </summary>
        public string Attachment { get; set; }

        /// <summary>
        /// 邮件编码
        /// </summary>
        public string Encoding { get; set; }

        /// <summary>
        /// SMTP服务器地址
        /// </summary>
        public string SmtpServer { get; set; }

        /// <summary>
        /// SMTP登录密码
        /// </summary>
        public string Password { get; set; }

        /// <summary>
        /// 发件人别称
        /// </summary>
        public string FromDisplayName { get; set; }

        /// <summary>
        /// 默认构造方法
        /// </summary>
        public MailMessageSender() { }
        /// <summary>
        /// 带参数构造方法
        /// </summary>
        /// <param name="smtp"></param>
        /// <param name="uid"></param>
        /// <param name="password"></param>
        /// <param name="displayName">昵称</param>
        public MailMessageSender(string smtp, string uid, string password, string displayName)
        {
            if (smtp == null) throw new ArgumentNullException("SMTP", "SMTP服务器值不能是NULL");
            this.SmtpServer = smtp;
            this.From = uid;
            this.Password = password;
            this.FromDisplayName = displayName;
        }
        /// <summary>
        /// 带参数构造方法
        /// <para>smtpInfo参数顺序为{0}=smtp,{1}=uid,{2}=password,{3}=displayName</para>
        /// </summary>
        /// <param name="smtpInfo"></param>
        public MailMessageSender(params string[] smtpInfo) : this(smtpInfo[0], smtpInfo[1], smtpInfo[2], smtpInfo[3]) { }

        /// <summary>
        /// 发送电子邮件
        /// </summary>
        /// <param name="toEmail"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        public void SendEmail(string toEmail, string subject, string body)
        {
            if (toEmail == null || subject == null || body == null || toEmail.Length == 0) return;
            MailMessage mail = new MailMessage(new MailAddress(this.From, this.FromDisplayName), new MailAddress(toEmail));
            mail.Subject = subject;
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.Body = body;
            mail.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient(this.SmtpServer);
            smtp.Credentials = new NetworkCredential(this.From, this.Password);
            smtp.Send(mail);
        }
        /// <summary>
        /// 发送电子邮件
        /// </summary>
        public void SendEmail()
        {
            this.SendEmail(this.To, this.Subject, this.Body);
        }

        /// <summary>
        /// 添加收件人
        /// </summary>
        public void AddTo() { }

        /// <summary>
        /// 添加抄送收件人
        /// </summary>
        public void AddCC() { }

        /// <summary>
        /// 添加暗送收件人
        /// </summary>
        public void AddBcc() { }

        /// <summary>
        /// 添加附件
        /// </summary>
        public void AddAttachment() { }

        /// <summary>
        /// 测试服务器是否正常
        /// </summary>
        /// <param name="toEmail"></param>
        /// <returns></returns>
        public bool TestSmtp(string toEmail)
        {
            MailMessage mail = new MailMessage(new MailAddress(this.From, this.FromDisplayName), new MailAddress(toEmail));
            mail.Subject = "Smtp状态测试";
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.Body = "Smtp服务正常运行中";
            mail.IsBodyHtml = true;

            SmtpClient smtp = new SmtpClient(this.SmtpServer);
            smtp.Credentials = new NetworkCredential(this.From, this.Password);
            try
            {
                smtp.Send(mail);
                return true;
            }
            catch(System.Net.Mail.SmtpException)
            {
                return false;
            }
        }
        #endregion
    }
}
